package flc.ast.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.stark.calculator.tax.adapter.HeadViewHolder;
import com.stark.calculator.tax.adapter.TaxCalRetAdapter;
import com.stark.calculator.tax.model.WageRateBean;

import java.util.List;

import flc.ast.R;
import stark.common.basic.utils.ViewUtil;

public class IncomeKind1RetAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEAD = 0;
    private static final int TYPE_ITEM = 1;

    private List<WageRateBean> datas;

    public void setDatas(List<WageRateBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public List<WageRateBean> getDatas() {
        return datas;
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;
        if (datas.get(position).getType() == WageRateBean.Type.HEAD) {
            type = TYPE_HEAD;
        }
        return type;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == TYPE_HEAD) {
            holder = new HeadViewHolder(LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_income_kin1_ret_head, parent, false));
        } else {
            holder = new ItemViewHolder(LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_income_kind1_ret, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadViewHolder) {
            ((HeadViewHolder) holder).bindData(datas.get(position));
        } else {
            ((ItemViewHolder) holder).bindData(datas.get(position));
        }
    }

    @Override
    public int getItemCount() {
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle, tvPersonal, tvCompany;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvPersonal = itemView.findViewById(R.id.tv_personal);
            tvCompany = itemView.findViewById(R.id.tv_company);
        }

        public void bindData(WageRateBean bean) {
            ViewUtil.setViewText(tvTitle, bean.getName());
            ViewUtil.setViewText(tvPersonal, bean.getPersonal());
            ViewUtil.setViewText(tvCompany, bean.getCompany());
        }
    }
}
